package com.zaey.auto.utils;

import com.baidu.aip.ocr.AipOcr;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.zaey.auto.utils.AipUtils.*;

/**
 * @author JohnSoybean
 * @create 2020-05-23 0:58
 * @description
 */
public class FileOpt {

    /**
     *
     * @param source 不带/
     * @param target 不带/
     * @param keywords
     */
    public static void sortFile(String source, String target, String last, String[] keywords) {
        AipOcr client = getClient();
        List<File> fileList = (List<File>) FileUtils.listFiles(new File(source), new String[]{"jpg","jpeg","png","bmp"}, false);
        int dirNum = keywords.length;
        //初始化序号 创建文件夹
        int todayIdx = 1;
        String todayDir = target + "/微信主页";
        File file1 = new File(todayDir);
        file1.mkdir();
        String failDir = target + "/--未归档";
        File file2 = new File(failDir);
        file2.mkdir();
        String todayDirLast = last + "/微信主页";
        File file3 = new File(todayDirLast);
        file3.mkdir();
        String failDirLast = last + "/--未归档";
        File file4 = new File(failDirLast);
        file4.mkdir();
        List<Integer> idxList = new ArrayList<>();
        List<String> dirList = new ArrayList<>();
        for (int i = 0; i < dirNum; i++) {
            idxList.add(1);
            File file = new File(target + "/" + keywords[i]);
            new File(last + "/" + keywords[i]).mkdir();
            file.mkdir();
            dirList.add(file.getAbsolutePath());
        }

        for (File file : fileList) {
            //是否已归档
            boolean flag = false;
            String suffix = FilenameUtils.getExtension(file.getName());
            JSONObject res = requestNormal(client, file.getAbsolutePath());
            try {
                String error_msg = res.getString("error_msg");
                while (error_msg != null && !"".equals(error_msg)) {
                    res = requestNormal(client, file.getAbsolutePath());
                    error_msg = res.getString("error_msg");
                }
            } catch (Exception e) {
            }
            Integer isToday = isToday(res, keywords);
            if (isToday == 1) {
                //首页
                try {
                    FileUtils.copyFile(file, new File(todayDir + "/" + todayIdx + "." + suffix));
                    todayIdx++;
                    flag = true;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else if (isToday == 0) {
                return;
            } else if (isToday == -1) {
                //详情页
                for (int i = 0; i < dirNum; i++) {
                    if (isKeywordExist(res, keywords[i])) {
                        //归档
                        try {
                            FileUtils.copyFile(file, new File(dirList.get(i) + "/" + idxList.get(i) + "." + suffix));
                            idxList.set(i, idxList.get(i) + 1);
                            flag = true;
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            if (flag == false) {
                //未能归档文件
                try {
                    FileUtils.copyFileToDirectory(file, new File(failDir));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
    }


    /**
     * 待优化：
     * 1. 水印位置动态调节
     * 2. 文件序号续上复制到微信主页
     * 3. 详情页图片文件不更改文件名
     * 4. 点赞数相同文件命名规则改为（）
     * @param args
     */

    public static void main(String[] args) {
        String[] keywords = new String[]{
                "霸权时代",
                "不要低估"
        };

        sortFile("G:\\var\\src", "G:\\var\\tgt", "G:\\var\\last", keywords);
    }

}
